﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Routing;
using System.Web.Mvc;
using MvcEngine.Core.Services;
using MvcEngine.Mvc.Constraints;
using MvcEngine.Mvc.Extensions;
using MvcEngine.Mvc.Filters;
using MvcEngine.Core;
using MvcEngine.Mvc.StarterSite.Controllers;
using MvcEngine.Core.Content;
using MvcEngine.Mvc;
using MvcEngine.Contact.Models;
using MvcEngine.Contact.ModelBinders;
using MvcEngine.Core.Registry;
using MvcEngine.Contact.StarterSite.Controllers;
using MvcEngine.Mvc.Services;

namespace MvcEngine.Contact
{
    public class ContactControlService : ControlServiceBase
    {
        public override void RegisterBackofficeRoutes(AreaRegistrationContext context)
        {
             context.MapMvcEngineRoute(
                "BackOffice_Contact",
                "BackOffice/Contact/{ctrId}",
                new { controller = "Contact", action = "Settings"},
                new string[] { "MvcEngine.Contact.BackOffice.Controllers" }
            );
        }

        public override void RegisterStarterSiteRoutes(AreaRegistrationContext context)
        {
            context.MapModuleRoute(
                "ContactSendEmail",
                "{pageName}/SendEmail",
                new { actionName = "SendEmail" },
                new string[] { "MvcEngine.Mvc.StarterSite.Controllers" }
            );
        }

        public override IList<ControlCommand> GetCommands()
        {
            List<ControlCommand> commands = new List<ControlCommand>();
            commands.Add(new SettingsCommand() { Name = "Settings" });
            return commands;
        }

        public override void RegisterActionFilters(IActionFilterRegistry registry)
        {
            //BackOffice
            ControllerActionCriteria moduleCriteria = new ControllerActionCriteria();
            moduleCriteria.AddAction<MvcEngine.Contact.BackOffice.Controllers.ContactController>(s => s.Settings(0));
            moduleCriteria.AddAction<MvcEngine.Contact.BackOffice.Controllers.ContactController>(s => s.Settings(0, null));
            registry.Add(new[] { moduleCriteria }, new ModuleActionFilter());

            ControllerCriteria contentCriteria = new ControllerCriteria();
            contentCriteria.AddController(typeof(MvcEngine.Contact.StarterSite.Controllers.ContactController));
            registry.Add(new[] { contentCriteria }, typeof(ModuleActionFilter));

            //--------------------------------------------------------------------------------------
            //---------------------------------Permissions------------------------------------------
            //-------------------------------------------------------------------------------------
            ControllerCriteria viewCriteria = new ControllerCriteria();
            viewCriteria.AddController(typeof(MvcEngine.Contact.StarterSite.Controllers.ContactController));

            ControllerCriteria editCriteria = new ControllerCriteria();
            editCriteria.AddController(typeof(MvcEngine.Contact.BackOffice.Controllers.ContactController));


            registry.Add(new[] { editCriteria }, new RolePermissionAttribute(Constants.Roles.Administrators));
            registry.Add(new[] { viewCriteria }, new ModulePermissionAttribute(PermissionTypes.View));
            //--------------------------------------------------------------------------------------
            //---------------------------------End of permissions----------------------------------
            //--------------------------------------------------------------------------------------
        }

        public override void RegisterModelBinders(ModelBinderDictionary binders)
        {
            binders[typeof(MessageData)] = new ContactModelBinder();
            binders[typeof(ContactSettings)] = new SettingsModelBinder();
        }

        public override Type GetControllerType()
        {
            return typeof(ContactController);
        }

        public override string GetDefaultAction()
        {
            return "Index";
        }
    }
}
